﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace Jy123.Web.Letter
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AddComment : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            int userID;//暂时写死
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string strLetterID = context.Request["LetterID"];
            string strComment = HttpUtility.UrlDecode(context.Request["commentcontent"]);
            if (!string.IsNullOrEmpty(strLetterID) && !string.IsNullOrEmpty(strComment.Trim()))
            {
                int newCommentID = 0;
                Jy123.Model.User_LetterComment mLetterComment = new Jy123.Model.User_LetterComment();
                Jy123.BLL.User_LetterComment bllLetterComment = new Jy123.BLL.User_LetterComment();
                mLetterComment.FUserCommenter = userID;
                mLetterComment.FLetter = Convert.ToInt32(strLetterID);
                mLetterComment.CommentContent = strComment;
                mLetterComment.PostTime = DateTime.Now;
                mLetterComment.PostIP = Common.Public.GetClientIP();
                mLetterComment.IsLock = false;
                newCommentID = bllLetterComment.Add(mLetterComment);//添加评论
                if (newCommentID > 0)
                {
                    string strHtml = context.Cache["user_lettercommentlistli"].ToString();
                    string strDel = "<img src=\"/images/del_blog.gif\" alt=\"删除\" onclick=\"javascript:myAjax('/Letter/CommentDel.ashx','CommentID='+$CommentID$);\" />";
                    strHtml = strHtml.Replace("$imgdel$", strDel);
                    strHtml = strHtml.Replace("$CommentID$", newCommentID.ToString());
                    strHtml = strHtml.Replace("$UserID$",userID.ToString());
                    strHtml = strHtml.Replace("$UserName$",new Jy123.BLL.User_Main().GetModel(userID).UserName);
                    strHtml = strHtml.Replace("$LetterContent$",strComment);
                    strHtml = strHtml.Replace("$PostTime$",DateTime.Now.ToString());
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(strHtml);
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
